import QtQuick 1.1
import com.nokia.meego 1.0

import "delegates"
import "components"
import "OAuth"

import "js/Core.js" as Core
import "js/HelperLib.js" as Helper

Page {
    id: root

    property string name: "FavoritesPage"
    property string listId: ""

    tools: mainToolBar

    PageBackground { }

    PageHeader {
        id: pageHeader
        text: qsTr("Favorites")
    }

    OAuthXmlListModel {
        id: oauthXmlListModel

        oauthSource: "http://api.xrel.to/api/favs/lists.xml"
        query: "/fav_lists/fav_list"
        token: globalSettings.oauthToken
        secret: globalSettings.oauthSecret
        oauthEnabled: globalSettings.loggedIn

        XmlRole { name: "id"; query: "id/string()" }
        XmlRole { name: "name"; query: "name/string()" }
        XmlRole { name: "public"; query: "public/number()" }
        XmlRole { name: "notify"; query: "notify/number()" }
        XmlRole { name: "auto_read"; query: "auto_read/number()" }
        XmlRole { name: "description"; query: "description/string()" }
        XmlRole { name: "password_hash"; query: "password_hash/string()" }
        XmlRole { name: "entry_count"; query: "entry_count/number()" }
        XmlRole { name: "unread_releases"; query: "unread_releases/number()" }
    }

    OAuthXmlListModel {
        id: oauthXmlListEntriesModel

        oauthSource: "http://api.xrel.to/api/favs/list_entries.xml"
        query: "/fav_list_entries/ext_info"
        token: globalSettings.oauthToken
        secret: globalSettings.oauthSecret
        parameters: [ ["id", listId], ["get_releases", true] ]

        XmlRole { name: "type"; query: "type/string()" }
        XmlRole { name: "id"; query: "id/string()" }
        XmlRole { name: "title"; query: "title/string()" }
        XmlRole { name: "link_href"; query: "link_href/string()" }
        XmlRole { name: "genre"; query: "genre/string()" }
        XmlRole { name: "cover_url"; query: "cover_url/string()" }
        XmlRole { name: "alt_title"; query: "alt_title/string()" }
    }

    ListView {
        id: favoritesListsView
        anchors.top: pageHeader.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        height: 80
        cacheBuffer: model.count*80
        snapMode: ListView.SnapToItem
        highlightRangeMode: ListView.StrictlyEnforceRange

        clip: true

        model: oauthXmlListModel
        delegate: FavoritesDelegate { }

        onCurrentIndexChanged: listId = oauthXmlListModel.get(currentIndex).id

        MouseArea {
            anchors.fill: parent
            onClicked: favoritesDialog.open();
        }
    }

    Separator { anchors.bottom: favoritesListsView.bottom }

    ListView {
        id: listEntriesView
        anchors.top: favoritesListsView.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        cacheBuffer: model.count*80
        spacing: 6
        snapMode: ListView.SnapToItem

        clip: true

        model: oauthXmlListEntriesModel
        delegate: FavoritesEntriesDelegate { }

        function showProductInfo(id) {
            pageStack.push(Qt.resolvedUrl("ProductInfoPage.qml"), { id: id })
        }
    }

    SelectionDialog {
        id: favoritesDialog
        titleText: "Select a favorites list:"
        model: oauthXmlListModel
        selectedIndex: favoritesListsView.currentIndex

        onSelectedIndexChanged: {
            favoritesListsView.currentIndex = selectedIndex;
            selectedIndex = favoritesListsView.currentIndex;
        }
    }
}
